
<script type="text/javascript">
	function init() { if(top.uploadDone) top.uploadDone(); } //top means parent frame.
	window.onload=init;
</script>

<?php

	session_start();
	$userId=$_SESSION['userId'];

	require('inc/fnc.php');
    $uploaddir=userimagepath($userId);

	list($name,$result, $avatar_icon) = upload('img', $uploaddir, 'jpg,jpeg,png,gif,GIF,PNG,JPG,JPEG');
	if($name) { 									// Upload Successful   
		$details = stat("users/".$userId."/img/");
		print json_encode(array(
			"success"	=>	$result,
			"failure"	=>	false,
			"file_name"	=>	$name,					// Name of the file - JS should get this value
			"avatar_icon"	=>	$avatar_icon,
			"size"		=>	null,					// Size of the file - JS should get this as well.
		));
	} else { // Upload failed for some reason.
		print json_encode(array(
			"success"	=>	false,
			"failure"	=>	$result,
		));
	}

	function upload($file_id, $uploaddir, $types){
		// here the code should be like this if there is a folder exist then delete its content
		list($part1, $part2, $part3) = explode("/", $uploaddir);
			if(!is_dir($part1."/".$part2)){
				if (!mkdir($part1."/".$part2, 0777)) {
					 die('Failed to create folders...');
				}
				if (!mkdir($part1."/".$part2."/".$part3, 0777)) {
					 die('Failed to create folders...');
				}
				
			} elseif(!is_dir($part1."/".$part2."/".$part3)){
				if (!mkdir($part1."/".$part2."/".$part3, 0777)) {
					 die('Failed to create folders...');
				}
			}
			
			$result='';    
			if(!$_FILES[$file_id]['name']) return array('','No file specified');
		
			$file_title = $_FILES[$file_id]['name'];
			$file_names=$file_title;
			//Get file extension
			$ext_arr = split("\.",basename($file_title));
			$ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension
				
			$all_types = explode(",",strtolower($types));
			if($types) {
				if(in_array($ext,$all_types));
				else {
					$result = "'".$_FILES[$file_id]['name']."' is not a valid file.".$uploaddir." last"; //Show error if any.
					return array('','invalid');
				}
			}
		
			$temp = $_FILES["img"]["tmp_name"];
			$file_ref = $_FILES["img"]["name"];
			$file_location_ori = $uploaddir .'/'. basename($file_ref);
			$file_location = $uploaddir .'/dp.jpg';
			$file_location_change = $file_location_ori.".jpg";
			if (!move_uploaded_file($temp, $file_location_ori)) {
				echo "<p>Oops! Your file (".$file_ref.") was not uploaded.</p>";
				return array("","invalid");
			} else {
				$imgInfo = getimagesize($file_location_ori);
				$mime_type=$imgInfo['mime'];
				if($mime_type=="image/jpeg" || $mime_type=="image/png" || $mime_type=="image/gif") {
					crop($file_location_ori, $uploaddir.'/small', 40, 40);
					crop($file_location_ori, $uploaddir.'/dp', 150, 150);
				} else {
					return array("","invalid");
				}
			}
			userimagechange($_SESSION['userId'],$uploaddir);
			
		$time=time();	
		return array($uploaddir.'/dp.jpg?time='.$time, $result, $uploaddir.'/small.jpg?time='.$time,);
	}
	
	function crop($filename, $dst, $new_width, $new_height){
		list($width, $height) = getimagesize($filename);					//get width and height
		$imgInfo = getimagesize($filename);									//get mime type
		$mime_type=$imgInfo['mime'];
		$image_p = imagecreatetruecolor($new_width, $new_height);			//destination image
		
		if($mime_type=="image/jpeg")										//set proper mime type for destination
			$image = imagecreatefromjpeg($filename);
		elseif($mime_type=="image/png")
			$image = imagecreatefrompng($filename);
		elseif($mime_type=="image/gif")
			$image = imagecreatefromgif($filename);
			
		//resample
if($width==$height){ imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); }			//if 1:1
elseif($width>$height){ imagecopyresampled($image_p, $image,  0, 0, (($width-$height)/2), 0, $new_width, $new_height, $width-($width-$height), $height); }	//if width> height
elseif($width<$height){ imagecopyresampled($image_p, $image, 0, 0, 0, (($height-$width)/2), $new_width, $new_height, $width, $height-($height-$width)); }	// if height>width
		
		// Output
		imagejpeg($image_p, $dst.".jpg", 100);		//set default type to jpg
	}
?>



